package com.elitel.frame.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.service.CfgExtsqlService;
import com.elitel.frame.base.service.DBServerService;
import com.elitel.frame.base.service.PageDetailsInfoService;
import com.elitel.frame.business.service.CensusSearchService;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgExtend;
import com.elitel.frame.main.entity.CfgExtsql;
import com.elitel.frame.main.entity.ext.PageDetailsinfoExt;
import com.elitel.frame.main.entity.ext.PrjServiceExt;
import com.elitel.frame.main.entity.vo.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/sysPageDetailsInfo")
public class PageDetailsInfoController {
    private static final Logger logger = LoggerFactory.getLogger(SysPrjServiceController.class);

    @Autowired
    private PageDetailsInfoService pageDetailsInfoService;
    @Autowired
    private CfgExtsqlService cfgExtsqlService;
    @Autowired
    private CensusSearchService censusSearchService;

    @Autowired
    private RedisCacheComponent redisCacheComponent;


    /**
     * TODO: 获取服务列表
     * @param pcode
     * @param servertype
     * @return
     */
    @RequestMapping("/getServiceBytype")
    @ResponseBody
    public Object getServiceBytype(String pcode, @RequestParam(value = "servertype[]") String[] servertype){
        JsonResult jsonResult = new JsonResult();
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("pcode", pcode);
            params.put("servertype", servertype);
            List<PrjServiceExt> list = pageDetailsInfoService.getServiceBytype(params);
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("服务信息加载异常！");
            logger.error(e.getMessage(), e);
        }
        return jsonResult;
    }


    /**
     * TODO: 获取查询字段
     * @param serviceId
     * @return
     */
    @RequestMapping("/getAllQueryFieldByServiceId")
    @ResponseBody
    public Object getAllQueryFieldByServiceId(String serviceId, String pageCode) {
        JsonResult jr = new JsonResult();
        try {
            List<PageDetailsinfoExt> list = pageDetailsInfoService.getQueryFieldListByServiceId(serviceId,pageCode);
            jr.setData(list);
            jr.setSuccess(true);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("服务信息加载异常！");
            logger.error(e.getMessage(), e);
        }
        return jr;
    }

    /**
     * TODO: 获取单表列表字段
     * @param serviceId
     * @return
     */
    @RequestMapping("/getAllShowFieldByServiceId")
    @ResponseBody
    public Object getAllShowFieldByServiceId(String serviceId, String pageCode) {
        JsonResult jr = new JsonResult();
        try {
            List<PageDetailsinfoExt> list = pageDetailsInfoService.getAllShowFieldByServiceId(serviceId,pageCode);
            jr.setData(list);
            jr.setSuccess(true);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("服务信息加载异常！");
            logger.error(e.getMessage(), e);
        }
        return jr;
    }

    /**
     * TODO: 获取脚本列表字段
     * @param serviceId
     * @return
     */
    @RequestMapping("/getAllShowFieldExecSQLByServiceId")
    @ResponseBody
    public Object getAllShowFieldExecSQLByServiceId(String serviceId, String pageCode) {
        JsonResult jr = new JsonResult();
        try {
            List<PageDetailsinfoExt> list = pageDetailsInfoService.getAllShowFieldExecSQLByServiceId(serviceId,pageCode);
            jr.setData(list);
            jr.setSuccess(true);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("服务信息加载异常！");
            logger.error(e.getMessage(), e);
        }
        return jr;
    }

    /**
     * TODO: 获取编辑列表字段
     * @param serviceId
     * @return
     */
    @RequestMapping("/getEditExecSQLByServiceId")
    @ResponseBody
    public Object getEditExecSQLByServiceId(String serviceId, String pageCode) {
        JsonResult jr = new JsonResult();
        try {
            List<PageDetailsinfoExt> list = pageDetailsInfoService.getEditExecSQLByServiceId(serviceId,pageCode);
            jr.setData(list);
            jr.setSuccess(true);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("服务信息加载异常！");
            logger.error(e.getMessage(), e);
        }
        return jr;
    }


    /**
     * TODO: 批量添加页面详情配置表
     * @param pageCode
     * @param pageDetailsinfos
     * @return
     */
    @RequestMapping("/insertPageDetailsinfoBatch")
    @ResponseBody
    public Integer insertPageDetailsinfoBatch(String pageCode, String pageDetailsinfos) {
        Integer result = 0;
        try {
            // 先删除配置，再批量添加
            pageDetailsInfoService.deleteByPageCode(pageCode);
            Map<String, Object> params = new HashMap<>();
            params.put("pagecode",pageCode);
            params.put("list", JSONArray.parse(pageDetailsinfos));
            result = pageDetailsInfoService.insertPageDetailsinfoBatch(params);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * TODO: 执行SQL 语句
     * @param serviceId
     * @return
     */
    @RequestMapping("/executeSQLByServiceId")
    @ResponseBody
    public Object executeSQLByServiceId(String serviceId, String type ,String sql) {
        JsonResult jr = new JsonResult();
        CfgDb cfgDb = null;
        try {
            if (type.equals("singleTableServer")) {
                CfgExtend cfgExtend = redisCacheComponent.cfgExtendRedisCache(serviceId);
                cfgDb = redisCacheComponent.cfgDBRedisCache(cfgExtend.getDbKey());
            } else {
                CfgExtsql cfgExtsql = cfgExtsqlService.selectCfgSqlByPrimary(serviceId);
                cfgDb = redisCacheComponent.cfgDBRedisCache(cfgExtsql.getDtDbKey());
            }
            List<Map<String, Object>> maps = censusSearchService.executequerySql(cfgDb, sql);
            jr.setData(maps);
            jr.setSuccess(true);
        } catch (Exception e) {
            jr.setSuccess(false);
            jr.setMsg("服务信息加载异常！");
            logger.error(e.getMessage(), e);
        }
        return jr;
    }
}
